<template>
  <div class="hawkContent">
    <hawk-breadcrumb></hawk-breadcrumb>
    <el-row :gutter="20" style="padding-left: 20px">
      <el-col
        :span="4"
        style="float: right; text-align: right; padding-right: 30px"
      >
        <el-button type="hawkCreate" @click="editExtinguish"
          >添加消防器材</el-button
        >
        <el-button plain>导入</el-button>
      </el-col>
    </el-row>
    <hawk-table
      :table-data="tableData"
      :options="[
        { key: 'serial', label: '序号' },
        { key: 'itype', label: '器械类型' },
        { key: 'address', label: '使用位置' },
        { key: 'responsibles', label: '负责人' },
        { key: 'status', label: '有效期' },

        { key: 'handle', label: '操作' },
      ]"
    >
      <template #handle="{ row }">
        <span class="hawk-text-btn" @click="editExtinguish(row)">编辑</span>
        <span class="hawk-text-divider">|</span>
        <span class="hawk-text-btn" @click="delExtinguish(row)">删除</span>
        <span class="hawk-text-divider">|</span>
        <span class="hawk-text-btn" @click="editExtinguish(row, 1)">查看</span>
      </template>
    </hawk-table>
    <div class="bottom-btns">
      <div class="bottom-btns-left">
        <div class="bottom-btns-left-num">
          {{ multipleSelection.length }} 选择
        </div>
        <el-button type="hawkDelete">批量删除</el-button>
        <el-button plain>导出</el-button>
      </div>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.size"
        layout=" prev, pager, next,sizes"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="formTitle"
      :visible.sync="addDialogVisible"
      width="30%"
      :close-on-click-modal="false"
      @close="addDialogClosed"
    >
      <!-- 内容主题区 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        label-position="left"
        ref="addFormRef"
        label-width="120px"
      >
        <el-form-item label="序号" prop="serial">
          <el-input
            :readonly="formTitle == '查看消防器材'"
            v-model="addForm.serial"
          ></el-input>
        </el-form-item>
        <el-form-item label="有效期" prop="status">
          <el-input v-model="addForm.status"></el-input>
        </el-form-item>
        <el-form-item label="器械类型" prop="itype">
          <el-input v-model="addForm.itype"></el-input>
        </el-form-item>
        <el-form-item label="使用位置" prop="address">
          <el-input v-model="addForm.address"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="responsibles">
          <el-input v-model="addForm.responsibles"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
/**
* @author 肖帅
* @description 消防器材的视图层
* @api /cmdb-extinguish(后端接口地址)
* @since 2021-10-10 17:30
* @update 2021-10-12 15:00
* @modifier 王洪展
* @bug 
* @version V1.0.0
 */
import { getList, doDelete, doAdd, doEdit } from '@/api/extinguish'
import HawkTable from '@/components/table'
import HawkBreadcrumb from '@/components/breadcrumb/Breadcrumb'

export default {
  components: { HawkBreadcrumb, HawkTable },
  //import引入的组件需要注入到对象中才能使用",
  data() {
    return {
      tableData: [],
      formTitle: '添加消防器材',
      addDialogVisible: false,
      multipleSelection: [],
      addForm: {
        serial: '',
        status: '',
        itype: '',
        address: '',
        responsibles: '',
      },
      addFormRules: {
        serial: [{ required: true, message: '请输入序号', trigger: 'blur' }],
        status: [{ required: true, message: '请输入有效期', trigger: 'blur' }],
        itype: [{ required: true, message: '请输入类型', trigger: 'blur' }],
        address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
        responsibles: [
          { required: true, message: '请输入负责人', trigger: 'blur' },
        ],
      },
      total: 0,
      page: {
        current: 1,
        size: 10,
      },
    }
  },
  //监听属性 类似于data概念",
  computed: {
    cellStyle() {
      return {
        padding: '8px 0',
      }
    },
  },
  //监控data中的数据变化",
  watch: {},
  //方法集合",
  methods: {
    async init() {
      const res = await getList(this.page)
      this.tableData = res.list
      this.total = res.count
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.page.size = val
      this.init()
    },
    handleCurrentChange(val) {
      this.page.current = val
      this.init()
    },
    save() {
      this.$refs.addFormRef.validate(async (valid) => {
        // console.log(valid)
        if (!valid) return
        if (this.addForm.id) {
          await doEdit(this.addForm)
        } else {
          await doAdd(this.addForm)
        }
        this.addDialogVisible = false
        this.init()
      })
    },
    delExtinguish(row) {
      this.$confirm('此操作将删除该空调信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then( async () => {
           let ret = await doDelete(row.id)
          console.log(ret, '--------------ret----');

          if (ret.code == 20000) {

            let type = ''
            if (ret.data) {
              ret.message = '删除成功'
              type = 'success'
            } else {
              type = 'error'
            }

            this.$message({
              type: type,
              message: ret.message,
            })

            // console.log(this.multipleSelection.length);
            if (this.multipleSelection.length == this.tableData.length) {
              this.queryForm.page.current = this.queryForm.page.current - 1
              if (this.queryForm.page.current < 1) {
                this.queryForm.page.current = 1
              }
            }

            this.multipleSelection = []
            console.log('-------------init---');
            this.init()

          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    editExtinguish(row, tname) {
      if (!row.id) {
        this.formTitle = '添加消防器材'
      } else {
        if (tname) {
          this.formTitle = '查看消防器材信息'
          this.addForm = Object.assign({}, row)
        } else {
          this.formTitle = '修改消防器材信息'
          this.addForm = Object.assign({}, row)
        }
      }
      this.addDialogVisible = true
    },
    addDialogClosed() {
      this.addForm = this.$options.data().addForm
      this.addDialogVisible = false
    },
  },
  //生命周期 - 创建之前",数据模型未加载,方法未加载,html模板未加载
  beforeCreate() {},

  //生命周期 - 创建完成（可以访问当前this实例）",数据模型已加载，方法已加载,html模板已加载,html模板未渲染
  created() {
    this.init()
  },

  //生命周期 - 挂载完成（可以访问DOM元素）",html模板已渲染
  mounted() {},
}
</script>

<style lang="scss">

</style>
